package edu.scau.cxq.gp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.common.utils.WrapperUtils;
import edu.scau.cxq.gp.entity.Inventory;
import edu.scau.cxq.gp.quartz.entity.InventoryAlertBean;
import edu.scau.cxq.gp.quartz.entity.QuartzBean;
import edu.scau.cxq.gp.quartz.utils.QuartzUtils;
import edu.scau.cxq.gp.service.IInventoryService;
import edu.scau.cxq.gp.entity.vo.InventoryVO;
import org.quartz.Scheduler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @description 库存 前端控制器
 * @return
 * @author TLC
 */


@RestController
@ResponseResult
@RequestMapping("/inventory")
public class InventoryController {

    @Resource
    private IInventoryService inventoryService;

    @Resource
    private QuartzBean quartzBean;

    @Resource
    private Scheduler scheduler;

    @Resource
    private InventoryAlertBean inventoryAlertBean;

    /**
     * 分页查找库存
     *
     * @return
     */
    @GetMapping("/selectByPage")
    public IPage<Inventory> selectByPage(Integer currentPage, Integer size, Inventory inventory) {
        Page<Inventory> page = new Page<>(currentPage, size);
        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(inventory);
        return inventoryService.page(page, queryWrapper);
    }

    /**
     * 分页查找库存
     *
     * @return
     */
    @GetMapping("/select")
    public PageInfo<InventoryVO> select(Integer currentPage, Integer size, @RequestParam(value = "goodsId", required = false) Long goodsId,
                                        @RequestParam(value = "wareId", required = false) Long wareId) {
        PageInfo<InventoryVO> inventoryVOList = inventoryService.getInventoryVOList(currentPage, size, goodsId, wareId);
        System.out.println(inventoryVOList);
        return inventoryVOList;
    }

    /**
     * 創建庫存整理定時任務
     * @param data
     * @return
     */
    @PostMapping("/createSortInventoryJob")
    public boolean createSortInventoryJob(@RequestBody Map<String, Object> data) {
        try {
            quartzBean.setCronExpression((String) data.get("expression"));
            quartzBean.setStatus(1); // 1代表啓動了
            QuartzUtils.createScheduleJob(scheduler, quartzBean);
            System.out.println("cron表达式为" + quartzBean.getCronExpression());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 运行一次整理库存
     * @return
     */
    @GetMapping("/runOnceSortInventory")
    public boolean  runOnceSortInventory()  {
        try {
            Integer status = quartzBean.getStatus();
            quartzBean.setStatus(1);
            QuartzUtils.runOnce (scheduler,"sort-inventory");
            quartzBean.setStatus(status);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 修改定时策略
     *
     * @return
     */
    @PostMapping("/updateSortInventoryCron")
    public boolean updateSortInventoryCron(@RequestBody Map<String, Object> data) {
        try {
            quartzBean.setCronExpression((String) data.get("expression"));
            QuartzUtils.updateScheduleJob(scheduler, quartzBean);
            quartzBean.setStatus(1);
            System.out.println("cron表达式更新为" + quartzBean.getCronExpression());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 获取定时策略
     *
     * @return
     */
    @GetMapping("/getSortInventoryQuartzBean")
    public QuartzBean getSortInventoryCron() {
        return quartzBean;
    }

    /**
     * 整理库存
     *
     * @return
     */
    @GetMapping("/inventoryMerge")
    public boolean inventoryMerge() {
        try {
            inventoryService.sortInventory();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 暂停整理库存任务
     * @return
     */
    @GetMapping("/pauseSortInventory")
    public boolean  pauseSortInventory()  {
        try {
            quartzBean.setStatus(2); // 2 代表暫停了
            QuartzUtils.pauseScheduleJob (scheduler,"sort-inventory");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 恢复整理库存任务
     * @return
     */
    @GetMapping("/resumeSortInventory")
    public boolean  resumeSortInventory()  {
        try {
            quartzBean.setStatus(1);
            QuartzUtils.resumeScheduleJob(scheduler,"sort-inventory");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 获取库存预警对象
     * @return
     */
    @GetMapping("/getInventoryAlertBean")
    public InventoryAlertBean getInventoryAlertBean() {
        return inventoryAlertBean;
    }

    /**
     * 获取库存预警对象
     * @return
     */
    @PostMapping("/setInventoryAlertBean")
    public boolean setInventoryAlertBean(@RequestBody InventoryAlertBean inventoryAlertBean) {
        try {
            BeanUtils.copyProperties(inventoryAlertBean, this.inventoryAlertBean);
            System.out.println(this.inventoryAlertBean);
        } catch (Exception e) {
            System.out.println("更新库存预警对象失败");
            return false;
        }
        return true;
    }
}
